package com.yksj.healthtalk.ui.home;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabWidget;
import android.widget.TextView;

import com.yksj.healthtalk.adapter.FriendListAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.friend.FriendInformUI;
import com.yksj.healthtalk.ui.home.MyFriendListActivity.TabsAdapter.TabInfo;
import com.yksj.healthtalk.utils.FriendHttpUtil;

/**
 * 我的好友
 * 
 * @author Administrator
 * 
 */
@SuppressLint("ValidFragment")
public class MyFriendListActivity extends BaseFragmentActivity implements
		OnClickListener {
	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;
	TabHost mTabHost;
	private AppData mAppData;

	public interface OnFrindListClickListener {
		void onClick(int type, CustomerInfoEntity entity, boolean flag);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myzone_friendlist_layout);
		initData();
		initView();

	}

	public void initView() {
		mViewPager = (ViewPager) findViewById(R.id.view_pager);
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();

		mTabsAdapter = new TabsAdapter(this, mViewPager, mTabHost);

		Bundle bundle1 = new Bundle();

		bundle1.putString("lable", getString(R.string.friend_recent));
		bundle1.putString("number", mAppData.getChatList("4").size() + "");

		Bundle bundle2 = new Bundle();
		bundle2.putString("lable", getString(R.string.my_attention));
		bundle2.putString("number", mAppData.getChatList("5").size() + "");

		Bundle bundle3 = new Bundle();
		bundle3.putString("lable", getString(R.string.my_fensi));
		bundle3.putString("number", mAppData.getChatList("6").size() + "");

		Bundle bundle4 = new Bundle();
		bundle4.putString("lable", getString(R.string.weibo_haoyuo));
		bundle4.putString("number", mAppData.getChatList("7").size() + "");

		Bundle bundle5 = new Bundle();
		bundle5.putString("lable", getString(R.string.friend_blacklist));
		bundle5.putString("number", mAppData.getChatList("8").size() + "");

		mTabsAdapter.addTab(mTabHost.newTabSpec("4"), Fragment.class, bundle1);
		mTabsAdapter.addTab(mTabHost.newTabSpec("5"), Fragment.class, bundle2);
		mTabsAdapter.addTab(mTabHost.newTabSpec("6"), Fragment.class, bundle3);
		mTabsAdapter.addTab(mTabHost.newTabSpec("7"), Fragment.class, bundle4);
		mTabsAdapter.addTab(mTabHost.newTabSpec("8"), Fragment.class, bundle5);

		initTitle();
	}

	public void initData() {
		mAppData = HTalkApplication.getAppData();
	}

	public void initTitle() {
		findViewById(R.id.title_back).setOnClickListener(this);
		Button mTitleName = (Button) findViewById(R.id.title_ok);
		mTitleName.setText(getString(R.string.my_friend));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			this.finish();
			break;
		}

	}

	@Override
	public void overridePendingTransition(int enterAnim, int exitAnim) {
		super.overridePendingTransition(R.anim.slide_in_left,
				R.anim.slide_out_right);
	}

	protected static class TabsAdapter extends FragmentStatePagerAdapter
			implements ViewPager.OnPageChangeListener,
			TabHost.OnTabChangeListener {
		private final Activity mActivity;
		private final ViewPager mViewPager;
		private final TabHost mTabHost;

		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;
			private final TextView msgTextView;

			TabInfo(String _tag, Class<?> _class, Bundle _args,
					TextView textView) {
				tag = _tag;
				clss = _class;
				args = _args;
				msgTextView = textView;
			}
		}

		public static class TabContentFactoryImpl implements TabContentFactory {
			private final Context mContext;

			public TabContentFactoryImpl(Context context) {
				this.mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabsAdapter(FragmentActivity fragmentActivity,
				ViewPager viewPager, TabHost tabHost) {
			super(fragmentActivity.getSupportFragmentManager());
			this.mActivity = fragmentActivity;
			this.mViewPager = viewPager;
			this.mTabHost = tabHost;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
			tabHost.setOnTabChangedListener(this);
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> _class,
				Bundle _args) {
			View view = LayoutInflater.from(mActivity).inflate(
					R.layout.salon_tab_tag, null);
			tabSpec.setIndicator(view);
			tabSpec.setContent(new TabContentFactoryImpl(mActivity));
			TextView msgTextView = (TextView) view
					.findViewById(R.id.tag_mesg_number);
			TextView lableTextView = (TextView) view
					.findViewById(R.id.tag_lable);
			lableTextView.setText(_args.getString("lable"));
			String number = _args.getString("number");
			if (number != null)
				msgTextView.setText(number);

			mTabHost.addTab(tabSpec);
			TabInfo tabInfo = new TabInfo(tabSpec.getTag(), _class, _args,
					msgTextView);
			mTabs.add(tabInfo);

		}

		@Override
		public Fragment getItem(int arg0) {
			String tag = mTabs.get(arg0).tag;
			Fragment fragment = ArrayListFragment.newInstance(tag,
					mTabs.get(arg0), mTabs);
			return fragment;
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			TabWidget tabWidget = mTabHost.getTabWidget();
			int oldFocusability = tabWidget.getDescendantFocusability();
			tabWidget
					.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(arg0);
			tabWidget.setDescendantFocusability(oldFocusability);
		}

		@Override
		public void onTabChanged(String tabId) {
			int index = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(index);
		}
	}

	@SuppressLint("ValidFragment")
	public static class ArrayListFragment extends ListFragment {
		private String type;// 列表类型
		private List<CustomerInfoEntity> list;
		public static final String KEY = "key";
		private final TabInfo mTabInfo;
		private final ArrayList<TabInfo> tabInfos;
		private AppData mAppData;
		private List<CustomerInfoEntity> mInterFriendList;
		OnFrindListClickListener mFrindListClickListener = new OnFrindListClickListener() {
			@Override
			public void onClick(int type, CustomerInfoEntity entity,
					boolean flage) {
				if (mInterFriendList == null)
					mInterFriendList = mAppData.getInterestFriendList();
				if (flage) {// flage为false, 查看资料
					switch (type) {
					case 5:// 取消关注
						mInterFriendList.remove(entity);
						tabInfos.get(1).msgTextView.setText(mInterFriendList.size()+"");
						break;
					case 4:// 最近联系人
					case 6:// 我的粉丝
					case 7:// 微博好友
						if (mInterFriendList.contains(entity)) {
							mInterFriendList.remove(entity);
							tabInfos.get(1).msgTextView.setText(mInterFriendList
									.size() + "");
						} else {
							mInterFriendList.add(entity);
							tabInfos.get(1).msgTextView.setText(mInterFriendList
									.size() + "");
						}
						// mTabInfo.msgTextView.setText(mInterFriend.size()+"");
						break;
					case 8:// 取消拉黑
//						mAppData.getBlackFriendIdList().remove(entity.getId());
						break;
					}
				} else {
					Intent intent = new Intent(getActivity(),
							FriendInformUI.class);
					intent.putExtra("PersonInfo", entity);
					getActivity().startActivity(intent);
				}

			}
		};

		@SuppressLint("ValidFragment")
		public ArrayListFragment(String type, TabInfo tabInfo,
				ArrayList<TabInfo> tabInfos) {
			super();
			this.mTabInfo = tabInfo;
			this.type = type;
			this.tabInfos = tabInfos;
		}

		static ArrayListFragment newInstance(String type, TabInfo tabInfo,
				ArrayList<TabInfo> tabInfos) {
			return new ArrayListFragment(type, tabInfo, tabInfos);
		}

		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View view = inflater.inflate(R.layout.list_fragment_layout, null);
			return view;
		}

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			mAppData = HTalkApplication.getAppData();
			list = mAppData.getChatList(type);
			FriendListAdapter adapter = new FriendListAdapter(getActivity(),
					type, mAppData, mFrindListClickListener);
			setListAdapter(adapter);
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			CustomerInfoEntity mCustomerInfoName = list.get(position);
			FriendHttpUtil.onItemClick(getActivity(), mCustomerInfoName);
		}
	}

}
